package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.BeanUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciReinsuranceRiskInsuredMapper;
import com.zatech.cgnci.project.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分入分出险种标的表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-03-19
 */
@Service
public class CgnciReinsuranceRiskInsuredServiceImpl extends ServiceImpl<CgnciReinsuranceRiskInsuredMapper, CgnciReinsuranceRiskInsuredDO> implements CgnciReinsuranceRiskInsuredService {

    @Resource
    CgnciDirectInsuranceService directInsuranceService;
    @Resource
    CgnciDirectInsuranceRiskService directInsuranceRiskService;
    @Resource
    CgnciDirectInsuranceDetailService directInsuranceDetailService;
    @Resource
    CgnciBusinessProjectService businessProjectService;
    @Resource
    CgnciReinsuranceInfoService reinsuranceInfoService;
    @Resource
    CgnciReinsuranceRiskService reinsuranceRiskService;
    @Resource
    CgnciReinsuranceRiskLayerService reinsuranceRiskLayerService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciUnderwritingPolicyService underwritingPolicyService;

    @Override
    public void deleteReinsuranceRiskInsuredForBusiness(Long riskInsuredId, String type) {
        // 查询险种标的
        CgnciReinsuranceRiskInsuredDO riskInsuredDO = this.getById(riskInsuredId);
        if (VerifyUtils.isEmpty(riskInsuredDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "riskInsuredId有误");
        }
        if (VerifyUtils.isEmpty(riskInsuredDO.getRowKey())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "rowKey为空");
        }

        // 查询险种信息
        CgnciReinsuranceRiskDO reinsuranceRiskDO = reinsuranceRiskService.getById(riskInsuredDO.getReinsuranceRiskId());
        if (VerifyUtils.isEmpty(reinsuranceRiskDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到险种信息");
        }
        if (VerifyUtils.isEmpty(reinsuranceRiskDO.getRiskSubTypeCode())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "险种riskSubTypeCode为空");
        }

        switch (type) {
            case "1":
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO1 = reinsuranceInfoService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO1)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除分人险种标的
                this.deleteReinsuranceRiskInsured("1", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode(), riskInsuredDO.getRowKey());

                // 删除超净自留险种标的
                // underwritingPolicyService.deleteUnderwritingPolicyRisk(reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                // 删除临分分出险种标的
                this.deleteReinsuranceRiskInsured("3", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode(), riskInsuredDO.getRowKey());

                // 删除合约分出块
                // reinsuranceRiskService.deleteHyReinsuranceRisk("2", reinsuranceInfoDO1.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode());

                break;
            case "2":
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO2 = reinsuranceInfoService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO2)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除临分分出险种标的
                this.deleteReinsuranceRiskInsured("3", reinsuranceInfoDO2.getBusinessId(), reinsuranceRiskDO.getRiskSubTypeCode(), riskInsuredDO.getRowKey());
                break;
            case "3":
                // 查询分人/分出信息
                CgnciReinsurerDO reinsurerDO = reinsurerService.getById(reinsuranceRiskDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsurerDO)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人方案/再保人");
                }
                // 查询分人/分出信息
                CgnciReinsuranceInfoDO reinsuranceInfoDO3 = reinsuranceInfoService.getById(reinsurerDO.getReinsuranceId());
                if (VerifyUtils.isEmpty(reinsuranceInfoDO3)) {
                    throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到分人/分出信息");
                }
                // 删除分人方案/再保人险种标的
                this.deleteReinsurerRiskInsured(reinsuranceInfoDO3, reinsuranceRiskDO.getRiskSubTypeCode(), riskInsuredDO.getRowKey(), reinsurerDO.getReinsurerId());
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "type参数错误!");
        }
    }

    @Override
    public void deleteReinsuranceRiskInsured(String reinsuranceType, Long businessId, String riskSubTypeCode, String rowKey) {
        // 查询分入信息
        CgnciReinsuranceInfoDO reinsuranceInfoDO = reinsuranceInfoService.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, businessId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, reinsuranceType));
        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO)) {
            // 查询分入--险种信息
            CgnciReinsuranceRiskDO reinsuranceRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsuranceInfoDO.getReinsuranceId())
                    .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
            if (VerifyUtils.isNotEmpty(reinsuranceRiskDO)) {
                // 查询分入--险种--标的
                List<CgnciReinsuranceRiskInsuredDO> riskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskDO.getReinsuranceRiskId())
                        .eq(CgnciReinsuranceRiskInsuredDO::getRowKey, rowKey));
                if (VerifyUtils.isNotEmpty(riskInsuredDOS)) {
                    // 处理分入信息汇总字段
                    reinsuranceInfoService.updateReinsuranceColumn(riskInsuredDOS, reinsuranceInfoDO, null);
                    // 删除分入--险种--标的
                    this.removeByIds(riskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));
                }
            }

            // 删除分人方案/再保人险种标的
            this.deleteReinsurerRiskInsured(reinsuranceInfoDO, riskSubTypeCode, rowKey, null);
        }
    }

    @Override
    public void deleteReinsurerRiskInsured(CgnciReinsuranceInfoDO reinsuranceInfoDO, String riskSubTypeCode, String rowKey, Long reinsurerId) {
        // 查询分入--分入方案
        List<CgnciReinsurerDO> reinsurerList = reinsurerService.list(new LambdaQueryWrapper<CgnciReinsurerDO>()
                .eq(CgnciReinsurerDO::getReinsuranceId, reinsuranceInfoDO.getReinsuranceId())
                .eq(VerifyUtils.isNotEmpty(reinsurerId), CgnciReinsurerDO::getReinsurerId, reinsurerId));
        if (VerifyUtils.isNotEmpty(reinsurerList)) {
            reinsurerList.forEach(reinsurer -> {
                // 查询分入--分入方案--险种信息
                CgnciReinsuranceRiskDO reinsurerRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                        .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsurer.getReinsurerId())
                        .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
                if (VerifyUtils.isNotEmpty(reinsurerRiskDO)) {
                    // 查询分入--分入方案--险种信息--标的
                    List<CgnciReinsuranceRiskInsuredDO> reinsurerRiskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                            .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsurerRiskDO.getReinsuranceRiskId())
                            .eq(CgnciReinsuranceRiskInsuredDO::getRowKey, rowKey));
                    if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredDOS)) {
                        // 处理分入--分入方案汇总字段
                        reinsurerService.updateReinsurerColumn(reinsurerRiskInsuredDOS, reinsurer, null);
                        // 删除分入--分入方案--险种--标的
                        this.removeByIds(reinsurerRiskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));
                    }
                }
            });
        }
    }

    @Override
    public void deleteReinsuranceRiskInsuredByDirect(Long businessId, Long riskId, String riskSubTypeCode, String rowKey) {
        // 查询分入信息
        CgnciReinsuranceInfoDO divideInReinsuranceInfoDO = reinsuranceInfoService.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, businessId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, "1"));
        if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO)) {
            // 查询分入--险种信息
            CgnciReinsuranceRiskDO divideInReinsuranceRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, divideInReinsuranceInfoDO.getReinsuranceId())
                    .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
            if (VerifyUtils.isNotEmpty(divideInReinsuranceRiskDO)) {
                // 查询分入--险种--标的
                List<CgnciReinsuranceRiskInsuredDO> divideInRiskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, divideInReinsuranceRiskDO.getReinsuranceRiskId()));
                if (VerifyUtils.isNotEmpty(divideInRiskInsuredDOS)) {
                    // 如果该险种标的的险种是多标的，则需要重新计算该险种其他标的对应的分人信息以及分入方案的明细字段并汇总，并删除当前险种标的的数据
                    // 如果临分分出信息的分出价格确认为 1-按分入净保费，则需要重新计算该险种其他标的对应的分出信息以及分出再保人的明细字段并汇总，并删除当前险种标的的数据
                    // 否则直接删除分人的险种标的和分人方案险种标的，以及临分分出险种标的和再保人的险种标的

                    // 需要更新的险种标的下面的层明细字段
                    List<CgnciReinsuranceRiskInsuredDO> updateRiskInsuredS = divideInRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> !rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());
                    // 需要删除的险种标的
                    List<CgnciReinsuranceRiskInsuredDO> deleteRiskInsuredS = divideInRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());

                    // 如果需要更新的险种标的不为空,需要重新计算并汇总
                    if (VerifyUtils.isNotEmpty(updateRiskInsuredS)) {
                        // 获取该险种标的在直保信息的 直保保费（不含税）
                        List<CgnciDirectInsuranceDetailDO> detailDOS = directInsuranceDetailService.list(new LambdaQueryWrapper<CgnciDirectInsuranceDetailDO>()
                                .eq(CgnciDirectInsuranceDetailDO::getDirectId, riskId));
                        // 直保保费（不含税）
                        BigDecimal premiumExclTax = BeanUtils.sumBigDecimalProperty(detailDOS, CgnciDirectInsuranceDetailDO::getPremiumExclTax);
                        // 分入净保费
                        BigDecimal netPremium;
                        // 根据分人标的ids查询险种层
                        List<CgnciReinsuranceRiskLayerDO> divideInLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, divideInRiskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList())));
                        if (VerifyUtils.isNotEmpty(divideInLayers)) {
                            // 分入/分出毛保费
                            BigDecimal sourceGrossPremium = BeanUtils.sumBigDecimalProperty(divideInLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                            // 分入/分出手续费
                            BigDecimal sourceCommission = BeanUtils.sumBigDecimalProperty(divideInLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                            // 分入/分出净保费
                            BigDecimal sourceNetPremium = BeanUtils.sumBigDecimalProperty(divideInLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                            // 分入/分出再保经纪费
                            BigDecimal sourceBrokerFee = BeanUtils.sumBigDecimalProperty(divideInLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                            // 更新分入/分出信息汇总字段
                            if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalGrossPremium())) {
                                divideInReinsuranceInfoDO.setTotalGrossPremium(divideInReinsuranceInfoDO.getTotalGrossPremium().subtract(sourceGrossPremium));
                            }
                            if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalCommission())) {
                                divideInReinsuranceInfoDO.setTotalCommission(divideInReinsuranceInfoDO.getTotalCommission().subtract(sourceCommission));
                            }
                            if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalNetPremium())) {
                                divideInReinsuranceInfoDO.setTotalNetPremium(divideInReinsuranceInfoDO.getTotalNetPremium().subtract(sourceNetPremium));
                            }
                            if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalBrokerFee())) {
                                divideInReinsuranceInfoDO.setTotalBrokerFee(divideInReinsuranceInfoDO.getTotalBrokerFee().subtract(sourceBrokerFee));
                            }

                            List<Long> inRiskInsuredIdS = updateRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList());
                            if (VerifyUtils.isNotEmpty(inRiskInsuredIdS)) {
                                // 根据分人标的ids查询需要更新的险种层
                                List<CgnciReinsuranceRiskLayerDO> divideInUpdateLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                        .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, inRiskInsuredIdS));
                                if (VerifyUtils.isNotEmpty(divideInUpdateLayers)) {
                                    divideInUpdateLayers.forEach(layer -> {
                                        if ("1".equals(divideInReinsuranceInfoDO.getReinsuranceMethod())) {
                                            // 分入毛保费 = 直保保费（不含税）* 承接比例
                                            layer.setGrossPremium(premiumExclTax.multiply(VerifyUtils.isEmpty(layer.getShareRatio()) ? BigDecimal.ZERO : layer.getShareRatio()));
                                        } else {
                                            // 分入毛保费 = 直保保费（不含税）* 承接比例 * 层保费占比
                                            layer.setGrossPremium(premiumExclTax.multiply(VerifyUtils.isEmpty(layer.getShareRatio()) ? BigDecimal.ZERO : layer.getShareRatio())
                                                    .multiply(VerifyUtils.isEmpty(layer.getLayerPremiumRatio()) ? BigDecimal.ZERO : layer.getLayerPremiumRatio()));
                                        }
                                        // 分保手续费 = 分入毛保费*分保手续费率
                                        layer.setCommission(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideInReinsuranceInfoDO.getTotalBrokerCommissionRate()) ? BigDecimal.ZERO : divideInReinsuranceInfoDO.getTotalBrokerCommissionRate()));
                                        // 分入净保费 = 分入毛保费-分保手续费
                                        layer.setNetPremium(layer.getGrossPremium().subtract(layer.getCommission()));
                                        // 再保经纪费 = 分入毛保费*再保经纪费
                                        layer.setBrokerFee(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideInReinsuranceInfoDO.getBrokerFeeRate()) ? BigDecimal.ZERO : divideInReinsuranceInfoDO.getBrokerFeeRate()));
                                    });
                                    reinsuranceRiskLayerService.updateBatchById(divideInUpdateLayers);

                                    // 分入/分出毛保费
                                    BigDecimal grossPremium = BeanUtils.sumBigDecimalProperty(divideInUpdateLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                    // 分入/分出手续费
                                    BigDecimal commission = BeanUtils.sumBigDecimalProperty(divideInUpdateLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                    // 分入/分出净保费
                                    netPremium = BeanUtils.sumBigDecimalProperty(divideInUpdateLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                    // 分入/分出再保经纪费
                                    BigDecimal brokerFee = BeanUtils.sumBigDecimalProperty(divideInUpdateLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                    // 更新分入/分出信息汇总字段
                                    if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalGrossPremium())) {
                                        divideInReinsuranceInfoDO.setTotalGrossPremium(divideInReinsuranceInfoDO.getTotalGrossPremium().add(grossPremium));
                                    }
                                    if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalCommission())) {
                                        divideInReinsuranceInfoDO.setTotalCommission(divideInReinsuranceInfoDO.getTotalCommission().add(commission));
                                    }
                                    if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalNetPremium())) {
                                        divideInReinsuranceInfoDO.setTotalNetPremium(divideInReinsuranceInfoDO.getTotalNetPremium().add(netPremium));
                                    }
                                    if (VerifyUtils.isNotEmpty(divideInReinsuranceInfoDO.getTotalBrokerFee())) {
                                        divideInReinsuranceInfoDO.setTotalBrokerFee(divideInReinsuranceInfoDO.getTotalBrokerFee().add(brokerFee));
                                    }
                                } else {
                                    netPremium = BigDecimal.ZERO;
                                }
                            } else {
                                netPremium = BigDecimal.ZERO;
                            }

                            // 汇总分出字段汇总
                            reinsuranceInfoService.updateById(divideInReinsuranceInfoDO);
                        } else {
                            netPremium = BigDecimal.ZERO;
                        }
                        // 删除分入--险种--标的
                        this.removeByIds(deleteRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));


                        // 查询分入--分入方案
                        // 处理分入方案，分入方案的明细字段并汇总，并删除当前险种标的的数据
                        List<CgnciReinsurerDO> reinsurerList = reinsurerService.list(new LambdaQueryWrapper<CgnciReinsurerDO>()
                                .eq(CgnciReinsurerDO::getReinsuranceId, divideInReinsuranceInfoDO.getReinsuranceId()));
                        if (VerifyUtils.isNotEmpty(reinsurerList)) {
                            reinsurerList.forEach(reinsurer -> {
                                // 查询分入--分入方案--险种信息
                                CgnciReinsuranceRiskDO reinsurerRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                                        .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsurer.getReinsurerId())
                                        .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
                                if (VerifyUtils.isNotEmpty(reinsurerRiskDO)) {
                                    // 查询分入--分入方案--险种信息--标的
                                    List<CgnciReinsuranceRiskInsuredDO> reinsurerRiskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                                            .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsurerRiskDO.getReinsuranceRiskId()));
                                    if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredDOS)) {
                                        // 需要更新的险种标的下面的层明细字段
                                        List<CgnciReinsuranceRiskInsuredDO> updateReinsurerRiskInsuredS = reinsurerRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> !rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());
                                        // 需要更新的险种标的
                                        List<CgnciReinsuranceRiskInsuredDO> deleteReinsurerRiskInsuredS = reinsurerRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());

                                        // 根据分人方案标的ids查询险种层
                                        List<CgnciReinsuranceRiskLayerDO> divideInReinsurerLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                                .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, reinsurerRiskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList())));
                                        if (VerifyUtils.isNotEmpty(divideInReinsurerLayers)) {
                                            // 分入/分出毛保费
                                            BigDecimal sourceGrossPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                            // 分入/分出手续费
                                            BigDecimal sourceCommission = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                            // 分入/分出净保费
                                            BigDecimal sourceNetPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                            // 分入/分出再保经纪费
                                            BigDecimal sourceBrokerFee = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                            // 更新分入/分出信息汇总字段
                                            if (VerifyUtils.isNotEmpty(reinsurer.getEngagedGrossPremium())) {
                                                reinsurer.setEngagedGrossPremium(reinsurer.getEngagedGrossPremium().subtract(sourceGrossPremium));
                                            }
                                            if (VerifyUtils.isNotEmpty(reinsurer.getEngagedCommission())) {
                                                reinsurer.setEngagedCommission(reinsurer.getEngagedCommission().subtract(sourceCommission));
                                            }
                                            if (VerifyUtils.isNotEmpty(reinsurer.getEngagedNetPremium())) {
                                                reinsurer.setEngagedNetPremium(reinsurer.getEngagedNetPremium().subtract(sourceNetPremium));
                                            }
                                            if (VerifyUtils.isNotEmpty(reinsurer.getEngagedEconomy())) {
                                                reinsurer.setEngagedEconomy(reinsurer.getEngagedEconomy().subtract(sourceBrokerFee));
                                            }

                                            List<Long> reinsurerRiskInsuredIdS = updateReinsurerRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList());
                                            if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredIdS)) {
                                                // 根据分人标的ids查询需要更新的险种层
                                                List<CgnciReinsuranceRiskLayerDO> divideInReinsurerUpdateLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                                        .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, reinsurerRiskInsuredIdS));
                                                if (VerifyUtils.isNotEmpty(divideInReinsurerUpdateLayers)) {
                                                    divideInReinsurerUpdateLayers.forEach(layer -> {
                                                        // 分入毛保费 = 直保保费（不含税）*承接比例*层保费占比
                                                        if ("1".equals(divideInReinsuranceInfoDO.getReinsuranceMethod())) {
                                                            // 分入毛保费 = 直保保费（不含税）* 承接比例
                                                            layer.setGrossPremium(premiumExclTax.multiply(VerifyUtils.isEmpty(layer.getShareRatio()) ? BigDecimal.ZERO : layer.getShareRatio()));
                                                        } else {
                                                            // 分入毛保费 = 直保保费（不含税）* 承接比例 * 层保费占比
                                                            layer.setGrossPremium(premiumExclTax.multiply(VerifyUtils.isEmpty(layer.getShareRatio()) ? BigDecimal.ZERO : layer.getShareRatio())
                                                                    .multiply(VerifyUtils.isEmpty(layer.getLayerPremiumRatio()) ? BigDecimal.ZERO : layer.getLayerPremiumRatio()));
                                                        }
                                                        // 分保手续费 = 分入毛保费*分保手续费率
                                                        layer.setCommission(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideInReinsuranceInfoDO.getTotalBrokerCommissionRate()) ? BigDecimal.ZERO : divideInReinsuranceInfoDO.getTotalBrokerCommissionRate()));
                                                        // 分入净保费 = 分入毛保费-分保手续费
                                                        layer.setNetPremium(layer.getGrossPremium().subtract(layer.getCommission()));
                                                        // 再保经纪费 = 分入毛保费*再保经纪费
                                                        layer.setBrokerFee(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideInReinsuranceInfoDO.getTotalBrokerFee()) ? BigDecimal.ZERO : divideInReinsuranceInfoDO.getTotalBrokerFee()));
                                                    });
                                                    reinsuranceRiskLayerService.updateBatchById(divideInReinsurerUpdateLayers);

                                                    // 分入/分出毛保费
                                                    BigDecimal grossPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                                    // 分入/分出手续费
                                                    BigDecimal commission = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                                    // 分入/分出净保费
                                                    BigDecimal reinsurerNetPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                                    // 分入/分出再保经纪费
                                                    BigDecimal brokerFee = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                                    // 更新分入/分出信息汇总字段
                                                    if (VerifyUtils.isNotEmpty(reinsurer.getEngagedGrossPremium())) {
                                                        reinsurer.setEngagedGrossPremium(reinsurer.getEngagedGrossPremium().add(grossPremium));
                                                    }
                                                    if (VerifyUtils.isNotEmpty(reinsurer.getEngagedCommission())) {
                                                        reinsurer.setEngagedCommission(reinsurer.getEngagedCommission().add(commission));
                                                    }
                                                    if (VerifyUtils.isNotEmpty(reinsurer.getEngagedNetPremium())) {
                                                        reinsurer.setEngagedNetPremium(reinsurer.getEngagedNetPremium().add(reinsurerNetPremium));
                                                    }
                                                    if (VerifyUtils.isNotEmpty(reinsurer.getEngagedEconomy())) {
                                                        reinsurer.setEngagedEconomy(reinsurer.getEngagedEconomy().add(brokerFee));
                                                    }
                                                }

                                            }

                                            // 汇总分出字段汇总
                                            reinsurerService.updateById(reinsurer);
                                        }
                                        // 删除分入--分入方案--险种--标的
                                        this.removeByIds(deleteReinsurerRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));
                                    }
                                }
                            });
                        }


                        // 处理临分分出信息
                        // 如果临分分出信息的分出价格确认为 1-按分入净保费，则需要重新计算该险种其他标的对应的分出信息以及分出再保人的明细字段并汇总，并删除当前险种标的的数据
                        // 否则直接删除临分分出的险种标的和分出再保人险种标的，并汇总
                        CgnciReinsuranceInfoDO divideOutReinsuranceInfoDO = reinsuranceInfoService.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                                .eq(CgnciReinsuranceInfoDO::getBusinessId, businessId)
                                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, "3"));
                        if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO) && "1".equals(divideOutReinsuranceInfoDO.getPriceConfirmationType())) {
                            // 处理临分分出信息
                            // 查询分出--险种信息
                            CgnciReinsuranceRiskDO divideOutReinsuranceRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, divideOutReinsuranceInfoDO.getReinsuranceId())
                                    .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
                            if (VerifyUtils.isNotEmpty(divideInReinsuranceRiskDO)) {
                                // 查询分入--险种--标的
                                List<CgnciReinsuranceRiskInsuredDO> divideOutRiskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                                        .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, divideOutReinsuranceRiskDO.getReinsuranceRiskId()));
                                if (VerifyUtils.isNotEmpty(divideOutRiskInsuredDOS)) {
                                    List<CgnciReinsuranceRiskInsuredDO> updateOutRiskInsuredS = divideOutRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> !rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());
                                    // 需要更新的险种标的
                                    List<CgnciReinsuranceRiskInsuredDO> deleteOutRiskInsuredS = divideOutRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());

                                    // 根据分人标的ids查询险种层
                                    List<CgnciReinsuranceRiskLayerDO> divideOutLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, divideOutRiskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList())));
                                    if (VerifyUtils.isNotEmpty(divideOutLayers)) {
                                        // 分入/分出毛保费
                                        BigDecimal sourceGrossPremium = BeanUtils.sumBigDecimalProperty(divideOutLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                        // 分入/分出手续费
                                        BigDecimal sourceCommission = BeanUtils.sumBigDecimalProperty(divideOutLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                        // 分入/分出净保费
                                        BigDecimal sourceNetPremium = BeanUtils.sumBigDecimalProperty(divideOutLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                        // 分入/分出再保经纪费
                                        BigDecimal sourceBrokerFee = BeanUtils.sumBigDecimalProperty(divideOutLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                        // 更新分入/分出信息汇总字段
                                        if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalGrossPremium())) {
                                            divideOutReinsuranceInfoDO.setTotalGrossPremium(divideOutReinsuranceInfoDO.getTotalGrossPremium().subtract(sourceGrossPremium));
                                        }
                                        if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalCommission())) {
                                            divideOutReinsuranceInfoDO.setTotalCommission(divideOutReinsuranceInfoDO.getTotalCommission().subtract(sourceCommission));
                                        }
                                        if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalNetPremium())) {
                                            divideOutReinsuranceInfoDO.setTotalNetPremium(divideOutReinsuranceInfoDO.getTotalNetPremium().subtract(sourceNetPremium));
                                        }
                                        if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalBrokerFee())) {
                                            divideOutReinsuranceInfoDO.setTotalBrokerFee(divideOutReinsuranceInfoDO.getTotalBrokerFee().subtract(sourceBrokerFee));
                                        }

                                        // 根据分出标的ids查询需要更新的险种层
                                        List<Long> outRiskInsuredIdS = updateOutRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList());
                                        if (VerifyUtils.isNotEmpty(outRiskInsuredIdS)) {
                                            List<CgnciReinsuranceRiskLayerDO> divideOutUpdateLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                                    .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, outRiskInsuredIdS));
                                            if (VerifyUtils.isNotEmpty(divideOutUpdateLayers)) {
                                                divideOutUpdateLayers.forEach(layer -> {
                                                    // 分出毛保费 = 分入净保费*层分出比例
                                                    layer.setGrossPremium(netPremium.multiply(VerifyUtils.isEmpty(layer.getCededRatio()) ? BigDecimal.ZERO : layer.getCededRatio()));
                                                    // 分保手续费 = 分出毛保费*分保手续费率
                                                    layer.setCommission(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideOutReinsuranceInfoDO.getTotalBrokerCommissionRate()) ? BigDecimal.ZERO : divideOutReinsuranceInfoDO.getTotalBrokerCommissionRate()));
                                                    // 分出净保费 = 分出毛保费-分保手续费
                                                    layer.setNetPremium(layer.getGrossPremium().subtract(layer.getCommission()));
                                                    // 再保经纪费 = 分出毛保费*再保经纪费
                                                    layer.setBrokerFee(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideOutReinsuranceInfoDO.getBrokerFeeRate()) ? BigDecimal.ZERO : divideOutReinsuranceInfoDO.getBrokerFeeRate()));
                                                });
                                                reinsuranceRiskLayerService.updateBatchById(divideOutUpdateLayers);

                                                // 分入/分出毛保费
                                                BigDecimal grossPremium = BeanUtils.sumBigDecimalProperty(divideOutUpdateLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                                // 分入/分出手续费
                                                BigDecimal commission = BeanUtils.sumBigDecimalProperty(divideOutUpdateLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                                // 分入/分出净保费
                                                BigDecimal divideOutNetPremium = BeanUtils.sumBigDecimalProperty(divideOutUpdateLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                                // 分入/分出再保经纪费
                                                BigDecimal brokerFee = BeanUtils.sumBigDecimalProperty(divideOutUpdateLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                                // 更新分入/分出信息汇总字段
                                                if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalGrossPremium())) {
                                                    divideOutReinsuranceInfoDO.setTotalGrossPremium(divideOutReinsuranceInfoDO.getTotalGrossPremium().add(grossPremium));
                                                }
                                                if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalCommission())) {
                                                    divideOutReinsuranceInfoDO.setTotalCommission(divideOutReinsuranceInfoDO.getTotalCommission().add(commission));
                                                }
                                                if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalNetPremium())) {
                                                    divideOutReinsuranceInfoDO.setTotalNetPremium(divideOutReinsuranceInfoDO.getTotalNetPremium().add(divideOutNetPremium));
                                                }
                                                if (VerifyUtils.isNotEmpty(divideOutReinsuranceInfoDO.getTotalBrokerFee())) {
                                                    divideOutReinsuranceInfoDO.setTotalBrokerFee(divideOutReinsuranceInfoDO.getTotalBrokerFee().add(brokerFee));
                                                }
                                            }
                                        }

                                        // 汇总分出字段汇总
                                        reinsuranceInfoService.updateById(divideOutReinsuranceInfoDO);
                                    }
                                    // 删除分入--险种--标的
                                    this.removeByIds(deleteOutRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));

                                    // 处理分入方案，分入方案的明细字段并汇总，并删除当前险种标的的数据
                                    // 查询分入--分入方案
                                    List<CgnciReinsurerDO> divideOutReinsurerList = reinsurerService.list(new LambdaQueryWrapper<CgnciReinsurerDO>()
                                            .eq(CgnciReinsurerDO::getReinsuranceId, divideOutReinsuranceInfoDO.getReinsuranceId()));
                                    if (VerifyUtils.isNotEmpty(divideOutReinsurerList)) {
                                        divideOutReinsurerList.forEach(reinsurer -> {
                                            // 查询分入--分入方案--险种信息
                                            CgnciReinsuranceRiskDO reinsurerRiskDO = reinsuranceRiskService.getOne(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                                                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, reinsurer.getReinsurerId())
                                                    .eq(CgnciReinsuranceRiskDO::getRiskSubTypeCode, riskSubTypeCode));
                                            if (VerifyUtils.isNotEmpty(reinsurerRiskDO)) {
                                                // 查询分入--分入方案--险种信息--标的
                                                List<CgnciReinsuranceRiskInsuredDO> reinsurerRiskInsuredDOS = this.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                                                        .eq(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsurerRiskDO.getReinsuranceRiskId()));
                                                if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredDOS)) {
                                                    // 需要更新的险种标的下面的层明细字段
                                                    List<CgnciReinsuranceRiskInsuredDO> updateReinsurerRiskInsuredS = reinsurerRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> !rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());
                                                    // 需要更新的险种标的
                                                    List<CgnciReinsuranceRiskInsuredDO> deleteReinsurerRiskInsuredS = reinsurerRiskInsuredDOS.stream().filter(divideInRiskInsuredDO -> rowKey.equals(divideInRiskInsuredDO.getRowKey())).collect(Collectors.toList());

                                                    // 根据分人方案标的ids查询险种层
                                                    List<CgnciReinsuranceRiskLayerDO> divideInReinsurerLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, reinsurerRiskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList())));
                                                    if (VerifyUtils.isNotEmpty(divideInReinsurerLayers)) {
                                                        // 分入/分出毛保费
                                                        BigDecimal sourceGrossPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                                        // 分入/分出手续费
                                                        BigDecimal sourceCommission = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                                        // 分入/分出净保费
                                                        BigDecimal sourceNetPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                                        // 分入/分出再保经纪费
                                                        BigDecimal sourceBrokerFee = BeanUtils.sumBigDecimalProperty(divideInReinsurerLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                                        // 更新分入/分出信息汇总字段
                                                        if (VerifyUtils.isNotEmpty(reinsurer.getEngagedGrossPremium())) {
                                                            reinsurer.setEngagedGrossPremium(reinsurer.getEngagedGrossPremium().subtract(sourceGrossPremium));
                                                        }
                                                        if (VerifyUtils.isNotEmpty(reinsurer.getEngagedCommission())) {
                                                            reinsurer.setEngagedCommission(reinsurer.getEngagedCommission().subtract(sourceCommission));
                                                        }
                                                        if (VerifyUtils.isNotEmpty(reinsurer.getEngagedNetPremium())) {
                                                            reinsurer.setEngagedNetPremium(reinsurer.getEngagedNetPremium().subtract(sourceNetPremium));
                                                        }
                                                        if (VerifyUtils.isNotEmpty(reinsurer.getEngagedEconomy())) {
                                                            reinsurer.setEngagedEconomy(reinsurer.getEngagedEconomy().subtract(sourceBrokerFee));
                                                        }


                                                        List<Long> reinsurerRiskInsuredIdS = updateReinsurerRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList());
                                                        if (VerifyUtils.isNotEmpty(reinsurerRiskInsuredIdS)) {
                                                            // 根据分人标的ids查询需要更新的险种层
                                                            List<CgnciReinsuranceRiskLayerDO> divideInReinsurerUpdateLayers = reinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                                                                    .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, reinsurerRiskInsuredIdS));
                                                            if (VerifyUtils.isNotEmpty(divideInReinsurerUpdateLayers)) {
                                                                divideInReinsurerUpdateLayers.forEach(layer -> {
                                                                    // 分出毛保费 = 分入净保费*层分出比例
                                                                    layer.setGrossPremium(netPremium.multiply(VerifyUtils.isEmpty(layer.getCededRatio()) ? BigDecimal.ZERO : layer.getCededRatio()));
                                                                    // 分保手续费 = 分出毛保费*分保手续费率
                                                                    layer.setCommission(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(reinsurer.getProcedureRate()) ? BigDecimal.ZERO : reinsurer.getProcedureRate()));
                                                                    // 分出净保费 = 分出毛保费-分保手续费
                                                                    layer.setNetPremium(layer.getGrossPremium().subtract(layer.getCommission()));
                                                                    // 再保经纪费 = 分出毛保费*再保经纪费
                                                                    layer.setBrokerFee(layer.getGrossPremium().multiply(VerifyUtils.isEmpty(divideOutReinsuranceInfoDO.getBrokerFeeRate()) ? BigDecimal.ZERO : divideOutReinsuranceInfoDO.getBrokerFeeRate()));
                                                                });
                                                                reinsuranceRiskLayerService.updateBatchById(divideInReinsurerUpdateLayers);

                                                                // 分入/分出毛保费
                                                                BigDecimal grossPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
                                                                // 分入/分出手续费
                                                                BigDecimal commission = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getCommission);
                                                                // 分入/分出净保费
                                                                BigDecimal reinsurerNetPremium = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getNetPremium);
                                                                // 分入/分出再保经纪费
                                                                BigDecimal brokerFee = BeanUtils.sumBigDecimalProperty(divideInReinsurerUpdateLayers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
                                                                // 更新分入/分出信息汇总字段
                                                                if (VerifyUtils.isNotEmpty(reinsurer.getEngagedGrossPremium())) {
                                                                    reinsurer.setEngagedGrossPremium(reinsurer.getEngagedGrossPremium().add(grossPremium));
                                                                }
                                                                if (VerifyUtils.isNotEmpty(reinsurer.getEngagedCommission())) {
                                                                    reinsurer.setEngagedCommission(reinsurer.getEngagedCommission().add(commission));
                                                                }
                                                                if (VerifyUtils.isNotEmpty(reinsurer.getEngagedNetPremium())) {
                                                                    reinsurer.setEngagedNetPremium(reinsurer.getEngagedNetPremium().add(reinsurerNetPremium));
                                                                }
                                                                if (VerifyUtils.isNotEmpty(reinsurer.getEngagedEconomy())) {
                                                                    reinsurer.setEngagedEconomy(reinsurer.getEngagedEconomy().add(brokerFee));
                                                                }
                                                            }
                                                        }

                                                        // 汇总分出字段汇总
                                                        reinsurerService.updateById(reinsurer);
                                                    }
                                                    // 删除分出--再保人--险种--标的
                                                    this.removeByIds(deleteReinsurerRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        } else {
                            // 删除临分分出信息
                            this.deleteReinsuranceRiskInsured("3", businessId, riskSubTypeCode, rowKey);
                        }
                    } else {
                        if (VerifyUtils.isNotEmpty(deleteRiskInsuredS)) {
                            // 处理分入信息汇总字段
                            reinsuranceInfoService.updateReinsuranceColumn(deleteRiskInsuredS, divideInReinsuranceInfoDO, null);
                            // 删除分入--险种--标的
                            this.removeByIds(deleteRiskInsuredS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()));
                        }
                        // 删除分人方案/再保人险种标的
                        this.deleteReinsurerRiskInsured(divideInReinsuranceInfoDO, riskSubTypeCode, rowKey, null);

                        // 删除临分分出信息
                        this.deleteReinsuranceRiskInsured("3", businessId, riskSubTypeCode, rowKey);
                    }
                }
            }

        }
    }

    @Override
    public void deleteReinsuranceRiskInsuredForDivideOut(Long riskInsuredId) {
        // 查询合约分出分项险种标的
        CgnciReinsuranceRiskInsuredDO riskInsuredDO = this.getById(riskInsuredId);
        if (VerifyUtils.isEmpty(riskInsuredDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "riskInsuredId有误");
        }
        // 删除合约分出分项险种标的
        this.removeById(riskInsuredId);
    }
}
